package com.bzxgc.sgwer.common.util;

import java.io.Serializable;
import java.util.List;

public class PageData<T> implements Serializable {
    /**
     * 每页显示记录的条数
     */
    private Integer pageSize;
    /**
     * 当前第几页
     */
    private Integer currentPage;
    /**
     * 总页数
     */
    private Integer totalPage;
    /**
     * 总记录条数
     */
    private Integer totalRecord;
    /**
     * 数据集合
     */
    private List<T> dataList;

    public PageData() {
    }

    public PageData(Integer pageSize, Integer currentPage, Integer totalPage, Integer totalRecord, List<T> dataList) {
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.totalPage = totalPage;
        this.totalRecord = totalRecord;
        this.dataList = dataList;
    }

    public PageData(Integer pageSize, Integer currentPage, Integer totalRecord, List<T> dataList) {
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.totalPage = totalPage(pageSize, totalRecord);
        this.totalRecord = totalRecord;
        this.dataList = dataList;
    }

    /**
     * 获取总页数
     * @param pageSize
     * @param totalRecord
     * @return
     */
    private Integer totalPage(Integer pageSize, Integer totalRecord) {
        int totalPage = totalRecord / pageSize;
        if (totalRecord % pageSize != 0) {
            totalPage++;
        }
        return totalPage;
    }

    /**
     * 获取起始索引
     * @param pageNum
     * @param pageSize
     * @return
     */
    public static Integer fromIndex(Integer pageNum, Integer pageSize) {
        return pageSize * (pageNum - 1);
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Integer getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(Integer totalRecord) {
        this.totalRecord = totalRecord;
    }

    public List<T> getDataList() {
        return dataList;
    }

    public void setDataList(List<T> dataList) {
        this.dataList = dataList;
    }
}
